package com.aaa.javaweb.servlet;

import com.aaa.javaweb.dao.UserDLDao;
import com.aaa.javaweb.dao.impl.RoomDaoImpl;
import com.aaa.javaweb.dao.impl.UserDLDaoimpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/webdenglu")
public class WebdengluContorller extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method!=null){
            switch (method){
                case "out":
                    this.dlout(req,resp);
                    break;
                case  "room":
                    this.room(req,resp);
                    break;
            }


        }else {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=UTF-8");
            String name = req.getParameter("uname");
            String password = req.getParameter("password");

            HttpSession session = req.getSession();
            UserDLDaoimpl userDLDaoimpl = new UserDLDaoimpl();
            List<Map> selDL = userDLDaoimpl.selDL();
            int a =0;
            //遍历返回的list<map>
            for (Map map : selDL) {
                //System.out.println(map.get("name"));
                if (map.get("name").equals(name) && map.get("pwd").equals(password) && map.get("status").equals(0)){
                    //代表登录成功
                    session.setAttribute("uname",name);
                    session.setAttribute("password",password);
                    String id = map.get("id")+"";
                    session.setAttribute("kid",id);
                    //否则就跳转到
                    Tiaozhuan(req,resp);
                    a=1;
                }

            }
            if (a==0){
                resp.sendRedirect("qianduan/webdenglu.jsp");
            }
        }



    }

    private void Tiaozhuan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("qianduan/jiudianshow.jsp").forward(req, resp);
    }

    private void room(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        RoomDaoImpl roomDao = new RoomDaoImpl();
        List<Map> roomdao =roomDao.selroom();
        String sss = JSON.toJSONString(roomdao);
        PrintWriter writer = resp.getWriter();
        writer.print(sss);
        writer.flush();
        writer.close();

    }

    private void dlout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        //session失效
        session.invalidate();

        resp.sendRedirect("qianduan/webdenglu.jsp");

    }
}